package club.pinea.util;

import club.pinea.model.User;
import club.pinea.proto.LobbyProto;
import club.pinea.session.ClientPlayer;
import club.pinea.session.MessageSend;
import club.pinea.utils.DateUtil;
import org.apache.commons.lang3.time.DateUtils;

import java.util.*;

/**
 * 打印信息封装
 */
public class PrintUtil {

    /**
     * 一对
     */
    public static final int TYPE_DOUBLE = 1;
    /**
     * 两对
     */
    public static final int TYPE_TOW_DOUBLE = 2;
    /**
     * 三张
     */
    public static final int TYPE_THIRD = 3;
    /**
     * 顺子
     */
    public static final int TYPE_STR = 4;
    /**
     * 同花
     */
    public static final int TYPE_SAME = 5;
    /**
     * 葫芦
     */
    public static final int TYPE_T_D = 6;
    /**
     * 4条
     */
    public static final int TYPE_FOUR = 7;
    /**
     * 同花顺
     */
    public static final int TYPE_SAME_STR = 8;
    /**
     * 同花大顺
     */
    public static final int TYPE_SAME_STR_BIG = 9;
    /**
     * 五条
     */
    public static final int TYPE_FIVE = 10;

    /**
     * 最小可奖励对的值（小于10不奖励）
     */
    public static final int MinDouble = 0;


    /**
     * 控制台输入类
     */
    private static Scanner scanner = new Scanner(System.in);

    private static String[] floorStr = new String[]{"♠ ", "♥ ", "♦ ", "♣ "};

    /**
     * 打印欢迎信息
     */
    public static void printHello() {
        System.out.println("***************************************");
        System.out.println("*                                     *");
        System.out.println("*       欢迎使用葫芦机游戏机          *");
        System.out.println("*                                     *");
        System.out.println("***************************************");
    }

    /**
     * 打印登录提示并返回登录信息
     */
    public static User printLoginForm() {
        System.out.println("请输入您的登录账号：");
        String name = scanner.next();
        System.out.println("请输入您的登录密码：");
        String pass = scanner.next();
        User user = new User();
        user.setUsername(name);
        user.setPassword(pass);
        return user;
    }


    /**
     * @description: 打印 创建用户 提示并返回信息
     * @author: 华上进
     * @createTime: 2021/7/2 14:25
     */
    public static User printRegForm() {
        System.out.println("*请输入创建登录账号：");
        String name = scanner.next();
        System.out.println("*请输入创建初始登录密码：");
        String pass = scanner.next();
        System.out.println("请输入邀请码（可不输入）：");
        String beInvited = scanner.next();
        User user = new User();
        user.setUsername(name);
        user.setPassword(pass);
        user.setBeInvited(beInvited);
        return user;
    }

    /**
     * @description: 修改密码
     * @author: 华上进
     * @createTime: 2021/7/2 15:58
     */
    public static Map<String, String> updatePasswordReturn() {
        Map<String, String> map = new HashMap<>();
        System.out.println("*请输入旧密码：");
        String oldPassword = scanner.next();
        System.out.println("*请输入新密码：");
        String newPassword = scanner.next();
        map.put("oldPassword", oldPassword);
        map.put("newPassword", newPassword);
        return map;
    }

    /**
     * 构建房间请求的提示信息
     *
     * @return
     */
    public static LobbyProto.RoomRequest printRoomForm() {
        System.out.println("当前服务器房间信息如下：");
        ServerResource.rooms.forEach(r -> {
            System.out.println(String.format(
                    "房间编号：%s，房间名称：%s，房间倍率：%d，房间座位数：%d，房间描述：%s",
                    r.getRoomId(), r.getRoomName(), r.getMultiplyPower(), r.getSeatCount(), r.getRoomDesc()
            ));
        });
        LobbyProto.RoomRequest.Builder request = LobbyProto.RoomRequest.newBuilder()
                .setUserId(ClientPlayer.getUserId());
        Integer roomId = nextScanInt("请输入房间编号进入房间", "输入有误，请重新输入：");
        request.setRoom(roomId);
        return request.build();
    }


    /**
     * 输入int类型
     *
     * @param tipMessage
     * @param errorMessage
     * @return
     */
    public static Integer nextScanInt(String tipMessage, String errorMessage) {
        System.out.println(tipMessage);
        while (true) {
            try {
                return scanner.nextInt();
            } catch (Exception e) {
                System.err.println(errorMessage);
            }
        }
    }

    /**
     * 输入int类型，可传入多个tipMessage
     *
     * @param tipMessageList
     * @param errorMessage
     * @return
     */
    public static Integer nextScanInt(List<String> tipMessageList, String errorMessage) {
        for (int i = 0; i < tipMessageList.size(); i++) {
            System.out.println(tipMessageList.get(i));
        }
        while (true) {
            try {
                return scanner.nextInt();
            } catch (Exception e) {
                System.err.println(errorMessage);
                return -1;
            }
        }
    }

    /**
     * 进入座位的输入信息
     *
     * @return
     */
    public static LobbyProto.SeatDownRequest printSeatForm() {
        System.out.println("当前房间座位信息如下：");
        ServerResource.seats.forEach(s -> {
            String tip = String.format(
                    "座位编号：%s，座位人：%s，座位状态：%s",
                    s.getSeatId(), s.getSeatUser(), formatSeatStatus(s.getSeatStatus()));
            if (s.getSeatStatus() == 2) {
                tip += "，留机时间：" + DateUtil.dateToString(new Date(s.getDate()));
            }
            System.out.println(tip);
        });
        int seatId = nextScanInt("请选择要进入的座位编号：", "输入错误，请输入要进入的座位编号");
        LobbyProto.SeatDownRequest request = LobbyProto.SeatDownRequest.newBuilder()
                .setSeatNum(seatId)
                .setRoomId(ClientPlayer.getRoomId())
                .setUserId(ClientPlayer.getUserId())
                .build();
        return request;
    }

    /**
     * 格式化座位状态描述
     *
     * @param status
     * @return
     */
    public static String formatSeatStatus(int status) {
        switch (status) {
            case 0:
                return "可坐";
            case 1:
                return "正在游戏";
            case 2:
                return "留机";
            default:
                return null;
        }
    }

    /**
     * 打印牌型
     *
     * @param i
     * @return
     */
    public static String card(int i) {
        if (i > 52) {
            return "『joker』";
        }
        int floor = i / 13;
        int num = i % 13;
        // 0 = 13 = A
        if (num == 0) {
            num = 13;
        }
        return "『" + floorStr[floor] + num + "』";
    }

    /**
     * 将类型转换为文字
     *
     * @param type
     * @return
     */
    public static String cardType(int type) {
        switch (type) {
            case TYPE_DOUBLE:
                return "一对";
            case TYPE_TOW_DOUBLE:
                return "两对";
            case TYPE_THIRD:
                return "三张";
            case TYPE_STR:
                return "顺子";
            case TYPE_SAME:
                return "同花";
            case TYPE_T_D:
                return "葫芦";
            case TYPE_FOUR:
                return "四条";
            case TYPE_SAME_STR:
                return "同花顺";
            case TYPE_SAME_STR_BIG:
                return "同花大顺";
            case TYPE_FIVE:
                return "五条";
            case MinDouble:
                return "散牌";
            default:
                return "散牌";
        }
    }
}
